package com.example.justbuy.action.buyer;
import com.example.justbuy.pojo.Address;
import com.example.justbuy.pojo.User;
import com.example.justbuy.service.AddressMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Controller
public class AddressAction {
    @Autowired
    private AddressMapperService addressMapperService;

    public void setAddressMapperService(AddressMapperService addressMapperService) {
        this.addressMapperService = addressMapperService;
    }

    public AddressMapperService getAddressMapperService() {
        return addressMapperService;
    }

    //地址页面跳转到编辑地址页面
    @RequestMapping("/Address.htm")
    public String Address(){
        return "AddressEdit.html";
    }

    //添加地址的请求及方法
    @RequestMapping("/AddressEdit.htm")
    public @ResponseBody String AddAddress(@RequestParam (value="consignee") String consignee,
                                           @RequestParam (value="phone") String phone,
                                           @RequestParam (value="checkaddress") String checkaddress,
                                           @RequestParam (value="detailed") String detailed,
                                           @RequestParam (value="defaultAddress") String defaultAddress,
                                           Model mod, HttpServletRequest request) throws Exception{
       String[] str= checkaddress.split(" ");
        //省
       String pareaid= str[0];
        //市
        String careaid= str[1];
        //县
        String areaid= str[2];
        //详细地址
        String address=pareaid+careaid+areaid+detailed;
        //添加时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String addTime = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        User user=(User)request.getSession().getAttribute("user");
       HttpSession session=  request.getSession();
        //获取用户名
       int userId=user.getId();
       HashMap map=new HashMap();
       map.put("addTime",addTime);
       map.put("userId",userId);
       map.put("detailed",detailed);
       map.put("address",address);
       map.put("consignee",consignee);
       map.put("phone",phone);
       map.put("defaultAddress",defaultAddress);
       map.put("pareaid",pareaid);
       map.put("careaid",careaid);
       map.put("areaid",areaid);
       if(defaultAddress.equals("是")){
           String defaul="否";
          int daaa= addressMapperService.SetDefaultAddress(defaul);
       }
      List<Address> alladdress=  addressMapperService.GetAllAddress(userId);
       if(alladdress.size()==0){
           defaultAddress="是";
           map.put("defaultAddress",defaultAddress);
       }
       int flag=addressMapperService.AddAddress(map);
       if(flag>0)
           return "1";
       else
            return "2";
    }
    //得到所有的地址
    @RequestMapping("/GetAllAddress.htm")
    public String GetAddress(Model model,HttpServletRequest request,String orderid) {
       //得到所有的地址
        User user=(User)request.getSession().getAttribute("user");
        HttpSession session=  request.getSession();
        //获取用户名
        int userId=user.getId();
       List<Address> listaddress= addressMapperService.GetAllAddress(userId);
       if(listaddress.size()>0){
           //得到默认地址
           HashMap map=new HashMap();
           //地址默认（只查询默认的地址）
           String defaultaddress="是";
           map.put("userId",userId);
           map.put("defaultaddress",defaultaddress);
           Address address= addressMapperService.GetDefaultAddress(map);
           //返回默认地址
           model.addAttribute("address",address);
           model.addAttribute("listaddress",listaddress);
       }
            model.addAttribute("orderid",orderid);
           return  "Address.html";
    }

    //修改地址的方法
@RequestMapping("/UpdateAddress.htm")
    public @ResponseBody String Update(@RequestParam (value="uid") int uid,
                         @RequestParam (value="consignee") String consignee,
                         @RequestParam (value="phone") String phone,
                         @RequestParam (value="checkaddress") String checkaddress,
                         @RequestParam (value="detailed") String detailed,
                         Model model){
        String[] str= checkaddress.split(" ");
        //省
        String pareaid= str[0];
        //市
        String careaid= str[1];
        //县
        String areaid= str[2];
        //详细地址
        String address=pareaid+careaid+areaid+detailed;
        Address add=new Address();
        add.setConsignee(consignee);
        add.setAddress(address);
        add.setAreaid(areaid);
        add.setPhone(phone);
        add.setCareaid(careaid);
        add.setPareaid(pareaid);
        add.setDetailed(detailed);
        add.setId(uid);
        int flag=addressMapperService.UpdateAddress(add);
        if(flag>0){
            return "1";
        }else{
            return "2";
        }
    }

    //获取修改地址的方法
    @RequestMapping("/getUpdateAddress.htm")
    public String GetUpdateAddress(@RequestParam(value="uid") int uid,Model model){
        Address address=addressMapperService.SearchAddress(uid);
        model.addAttribute("address",address);
        return "AddressUpdate.html";
    }

    //删除地址的方法
    @RequestMapping("/deleteAddress.htm")
    public @ResponseBody String deleteAddress(@RequestParam(value="did") int did,Model model,HttpServletRequest request){
        HashMap map1=new HashMap();
       int flag= addressMapperService.DeleteAddress(did);
       System.out.println(flag);
       if(flag>0){
           return  "1";
       }else{
           return  "2";
       }
    }

}
